const { Controller } = require("egg");

class RecommendGoodsController extends Controller {
  // 添加推荐商品
  async addRecommendGoods() {
    const { ctx } = this;
    const { cover, goodsId, categoryId, recommendDate, sort } = ctx.request.body;
    ctx.validate({
      cover: { type: "nullValue", fieldName: "封面图" },
      goodsId: { type: "nullValue", fieldName: "关联商品" },
      categoryId: { type: "nullValue", fieldName: "分类id" },
      recommendDate: { type: "nullValue", fieldName: "推荐时间" },
    });
    await ctx.model.RecommendGoods.create({
      cover,
      goodsId,
      categoryId,
      recommendDate,
      sort
    });
    ctx.send({ msg: "成功" });
  }
  //   获取推荐商品
  async getRecommendGoods() {
    const { ctx, service } = this;
    const res = await service.recommendGoods.getRecommendGoods();
    ctx.send(res);
  }
  // 删除推荐商品
  async delRecommendGoods() {
    const { ctx } = this;
    const { _id } = ctx.params;
    console.log(_id);
    await ctx.model.RecommendGoods.deleteOne({ _id });
    ctx.send({ msg: "成功" });
  }
  // 编辑推荐商品
  async editRecommendGoods() {
    const { ctx } = this;
    const { _id, saleIncr, visiTimes, recommendDate, sort, cover, goodsId, categoryId } = ctx.request.body;

    await ctx.model.RecommendGoods.updateOne({ _id }, { recommendDate, sort, cover, goodsId, categoryId });
    ctx.send({ msg: "成功" })
  }
  // 推荐商品访问次数与销量增长
  async monitorRecommendGoods() {
    const { ctx } = this;
    const { _id, saleIncr, visitTimes } = ctx.request.body;
    console.log("我是：", visitTimes);
    ctx.validate({
      _id: { type: "nullValue", fieldName: "_id" },
    });
    await ctx.model.RecommendGoods.updateOne({ _id }, { $inc: { visitTimes: visitTimes || 0, saleIncr: saleIncr || 0 } });
    ctx.send({ msg: "成功" })
  }
  // 首页轮播数据
  async getSwiper() {
    const { ctx } = this;
    const res = await ctx.model.RecommendGoods.find();
    ctx.send({ msg: "成功", data: res });
  }
}
module.exports = RecommendGoodsController;
